import pymysql
from dotenv import load_dotenv
import os

def get_connection():
    return pymysql.connect(
        host="localhost".encode('utf-8'),
        port=3306,
        user="root".encode('utf-8'),
        password="69296616".encode('utf-8'),
        database="dbkeshe".encode('utf-8'),
        cursorclass=pymysql.cursors.DictCursor
    )


try:
    # 获取数据库连接
    connection = get_connection()
    
    # 测试查询
    with connection.cursor() as cursor:
        cursor.execute("SELECT VERSION()")
        version = cursor.fetchone()
        print(f"MySQL数据库版本: {version['VERSION()']}")
        
    print("数据库连接成功！")
    
except Exception as e:
    print(f"数据库连接失败: {e}")
    
finally:
    if 'connection' in locals() and connection:
        connection.close()